package com.cslg.questionnaires.service;

import com.cslg.questionnaires.entity.Question;
import com.cslg.questionnaires.mapper.QuestionMapper;
import com.cslg.questionnaires.utils.QuestionUtils;
import com.cslg.questionnaires.utils.ServiceMessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.HashMap;

@Service
public class AddQuestionService {
    @Resource
    private QuestionMapper questionMapper;

    public HashMap<String, Object> saveQuestion(HashMap<String, Object> map,int owerid,String userType){
        Question questionForm;
        try {
            questionForm = QuestionUtils.buildQuestionForm(map, owerid);
        } catch (ParseException e) {
            return ServiceMessageBuilder
                    .buildMessage(500,e.toString(),null);
        }
        questionForm.setOwertype(userType);
        questionForm.setIsexamine(8);
        int row = questionMapper.addQuestion(questionForm);
        if (row == 1){
            return ServiceMessageBuilder
                    .buildMessage(200,"success",null);
        } else {
            return ServiceMessageBuilder
                    .buildMessage(502,"error",null);
        }
    }

    public HashMap<String, Object> publishQuestion(HashMap<String, Object> map,int owerid,String userType){
        Question questionForm;
        try {
            questionForm = QuestionUtils.buildQuestionForm(map, owerid);
        } catch (ParseException e) {
            return ServiceMessageBuilder
                    .buildMessage(500,e.toString(),null);
        }
        questionForm.setOwertype(userType);
        if (userType.equals("Teacher")) {
            questionForm.setIsexamine(1);
        }else {
            questionForm.setIsexamine(0);
        }
        int row = questionMapper.addQuestion(questionForm);
        if (row == 1){
            return ServiceMessageBuilder
                    .buildMessage(200,"success",null);
        } else {
            return ServiceMessageBuilder
                    .buildMessage(502,"error",null);
        }
    }
}
